<?php 
declare (strict_types = 1);
namespace app\api\controller;
use app\common\controller\Api;
use think\Request;
class BusinessHost extends Api
{
    protected $with = ['business_host_script'=>['script'=>['script_setting_nd','script_setting_xs','script_class.script_setting']]];
    //所有的主持人数据
    public function bus_all()
    {
        $this->businessCheckLogin();
        $data = $this->model->where([['business_id','=',$this->businessinfo['id']]])->order('id desc')->select();
        if(!empty($data) && !empty($data->toArray())){
            $data = $data->toArray();
        }
        return datasuccess($data);
    }
    //商户端-DM管理列表
    public function index()
    {
        $this->businessCheckLogin();
        $post = param();
        $limit = input('limit') ?: 20;
        $data = $this->model->where([['business_id','=',$this->businessinfo['id']]])->order('id desc')->paginate($limit);
        if(!empty($data) && !empty($data->toArray())){
            $data = $data->toArray();
            foreach ($data['data'] as $k=>$v){
                $image = $v['images'];
                if(!empty($image)){
                    $image = explode(',',$image);
                    foreach ($image as $kk=>$vv){
                        if(!strstr($vv,'http')){
                            $image[$kk] = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME'] . $vv;
                        }
                    }
                    $data['data'][$k]['images'] = $image;
                }else{
                    $data['data'][$k]['images'] = [];
                }
            }
            
        }
        urladd($data);
        return datasuccess($data);
    }
    //商户端-主持人编辑
    public function upload()
    {
        $this->businessCheckLogin();
        $post = param();
        if(!empty($post)){
            if(!isset($post['id'])) return msgerror('参数缺失');
            $this->model::update($post);
            return datasuccess();
        }else{
            return msgerror('参数缺失');
        }
    }
    //商户端-主持人添加
    public function add()
    {
        $this->businessCheckLogin();
        $post = param();
        if(!empty($post)){
            $post['business_id'] = $this->businessinfo['id'];
            $this->model->save($post);
            return datasuccess();
        }else{
            return msgerror('参数缺失');
        }
    }
    //商户端-主持人记录-全部主持人列表
    public function all()
    {
        $this->businessCheckLogin();
        $data = $this->model->where('business_id','=',$this->businessinfo['id'])->order('id desc')->select();
        return datasuccess($data);
    }
    //用户端-首页-主持人
    public function user_index()
    {
        $post = param();
        if(!isset($post['id'])) return msgerror('参数缺失');
        $data = $this->model->where('business_id','=',$post['id'])->order('id desc')->select();
        return datasuccess($data);
    }
    //通用-主持人-详情-基础信息
    public function bus_view()
    {
        if (!input('post.id')) {
            return msgerror('缺少参数id');
        }
        $this->where[] = ['id','=',input('id')];
        $data = $this->model->where($this->where)->with($this->with)->find()->toArray();
        new_urladd($data);
        return datasuccess($data);
    }
    //用户端-主持人-详情
    public function view()
    {
        if (!input('post.id')) {
            return msgerror('缺少参数id');
        }
        $this->where[] = ['id', '=', input('post.id')];
        $limit = input('limit') ?: 20;
        $list = $this->model->with($this->with)->where($this->where)->with($this->with)->find()->toArray();
        $BusinessHostComment_model = app('app\api\model\BusinessHostComment');
        $list['comment'] = $BusinessHostComment_model->where([
            ['business_host_id','=',input('post.id')],
            ['status','=',2]
        ])->with(['user_index'])->order('id desc')->paginate($limit);
        if(!empty($list['comment']) && !empty($list['comment']->toArray())){
            $list['comment'] = $list['comment']->toArray();
            foreach ($list['comment']['data'] as $k=>$v){
                if(!empty($v['images'])){
                    $list['comment']['data'][$k]['images'] = explode(',',$v['images']);
                }else{
                    $list['comment']['data'][$k]['images'] = [];
                }
            }
        }
        return datasuccess($list);
    }
}